<?php
/**
 * Note: 针对需要控制的支付进行管理, 目前只需要微信
 */
namespace App\Services;

use App\Abstracts\PayAbstract;
use App\Services\Pay\PayAdapay;
use App\Services\Pay\PayAlipay;
use App\Services\Pay\PayHelloNow;
use App\Services\Pay\PayWechat;
use App\Services\Pay\PayWechatProfitSharing;
use App\Services\Pay\PayYajie;
use Illuminate\Support\Str;

/**
 * phpstorm提示
 * @method static PayAlipay alipay() 支付宝
 * @method static PayWechat wechat() 微信
 * @method static PayHelloNow helloNow() 你好现在
 * @method static PayYajie yajie() 亚捷
 * @method static PayWechatProfitSharing wechatProfitSharing() wechatProfitSharing
 */
class PayService
{
    // protected $config;

    public function __construct()
    {
        // $this->config = $config;
    }

    /**
     * @param $method
     * @param $params
     * @return PayAbstract
     * @throws \Exception
     */
    public static function __callStatic($method, $params): PayAbstract
    {
        // $app = new self(...$params);
        $app = new self();

        return $app->create($method);
    }

    /**
     * @param string $method
     * @return PayAbstract
     * @throws \Exception
     */
    protected function create(string $method): PayAbstract
    {
        $gateway = __NAMESPACE__.'\\Pay\\Pay'.Str::studly($method);

        if (class_exists($gateway)) {
            return self::make($gateway);
        }

        throw new \Exception("Pay [{$method}] Not Exists");
    }

    /**
     * @param $gateway
     * @return PayAbstract
     * @throws \Exception
     */
    protected function make($gateway): PayAbstract
    {
        $app = new $gateway();

        if ($app instanceof PayAbstract) {
            return $app;
        }

        throw new \Exception("Pay [{$gateway}] Must Be An Extend Of PayAbstract");
    }
}
